package com.shilei.android;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.TextView;

public class ShileiActivity extends ListActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
        
		Class drawable = R.drawable.class;
		
		int image_id = 0;
		try {
        
			Field field = drawable.getDeclaredField("icon");
        
			image_id = field.getInt("icon");
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ImageView image = (ImageView) findViewById(R.id.user_image);
		TextView tx = (TextView)findViewById(R.id.user_name);
		//image.setImageResource(image_id);
		// ���һ��ArrayList���͵ı�list
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		// ���}��HashMap���͵ı�map1 �� map2
		// HashMpaΪ��ֵ�����͡���һ�����Ϊ�����ڶ������Ϊֵ
		HashMap<String, Object> map1 = new HashMap<String, Object>();
		HashMap<String, Object> map2 = new HashMap<String, Object>();
		// �������䵽map1��map2�С�
		map1.put("user_image", R.drawable.icon);
		map1.put("user_name", "石磊");
		map1.put("user_ip", "192.168.1.52");

		map1.put("user_image", R.drawable.icon);
		map2.put("user_name", "哈哈");
		map2.put("user_ip", "192.168.0.1");
		// ��map1��map2��ӵ�list��
		list.add(map1);
		list.add(map2);
		// ���һ��SimpleAdapter���͵ı�4������
		SimpleAdapter listAdapter = new SimpleAdapter(this, list,
				R.layout.user, new String[] { "user_image", "user_name", "user_ip" },
				new int[] { R.id.user_image, R.id.user_name, R.id.user_ip });
//		listAdapter.setViewBinder(new ViewBinder(){
//
//			@Override
//			public boolean setViewValue(View arg0, Object arg1, String arg2) {
//				// TODO Auto-generated method stub
//				return false;
//			}
//			
//		});
		// ������ʾListView
		setListAdapter(listAdapter);
	}

	// ��дonListItemClick����ListView��Ŀ�¼�
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		// ��ʾ������ĿID��
		System.out.println("id = " + id);
		// ��ʾ�����Ŀ��λ����
		System.out.println("position = " + position);
	}

	public Bitmap getBitmap() {
		Bitmap mBitmap = null;


			//URL url = new URL(this.getPackageResourcePath());
			//HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			//InputStream is = conn.getInputStream();
			InputStream is = this.getResources().openRawResource(R.drawable.icon);
			mBitmap = BitmapFactory.decodeStream(is);

		return mBitmap;
	}
}